﻿Partial Class Document

    Public Class Cell
        Dim Row As Document.Row
        Dim Column As Column
        Dim Value As Object
        Dim Font As Font
        Dim Bold As Boolean = False
        Dim Italic As Boolean = False
        Dim Align As String = ""
        Dim VerticalAlign As String = ""
        Dim Format As String
        Dim IsHeader As Boolean
        Dim FontSize As String
        Dim Indent As Single
        Dim Tram As String = ""
        Dim Scale As Single = 1
        Dim Type As String = "text"

        Public RowSpan As Integer = 1
        Public ColSpan As Integer = 1

        Sub New(ByVal Row As Document.Row, ByVal Value As Object, ByVal Column As Column, ByVal IsHeader As Boolean)
            Me.Row = Row
            Me.Column = Column
            Dim FontSize As String = Row.Table.FontSize
            Dim FontName As String = ReportFontRegular
            Align = Column.Alignment
            Do
                If Strings.Left(Value & "", 1) <> "<" Then Exit Do
                Value = Mid(Value, 2)
                Dim Pos As Integer = InStr(Value, ">")
                Dim Attrib = Split(Strings.Left(Value, Pos - 1), "=")
                Value = Mid(Value, Pos + 1)
                Select Case Attrib(0)
                    Case "rs", "rowspan" : RowSpan = Val(Attrib(1)) + 1
                    Case "cs", "colspan" : ColSpan = Val(Attrib(1))
                    Case "b" : Bold = True
                    Case "i" : Italic = True
                    Case "c", "l", "r" : Align = Attrib(0)
                    Case "t", "m", "b" : VerticalAlign = Attrib(0)
                    Case "align" : Align = Attrib(1)
                    Case "valign" : VerticalAlign = Attrib(1)
                    Case "img" : Type = "image"
                    Case "scale" : Scale = Attrib(1)
                        'Case "g" : Gray = Val(Attrib(1))
                        'Case "nb" : NoBorder = True
                    Case "-", "--", "---", "+", "++", "+++" : FontSize &= Attrib(0)
                        'Case "link" : Link = Attrib(1)
                        'Case "x" : Hidden = True
                    Case "w", "wrap" : Align &= "w"
                    Case "*" : Tram = Attrib(0)
                End Select
            Loop
            Dim Style As FontStyle = FontStyle.Regular
            'If Mid(Value, 1, 1) = "[" AndAlso Mid(Value, 3, 1) = "]" Then
            'FontName = "wingdings 2"
            'FontSize &= "++"
            'Select Case Mid(Value, 2, Len(Value) - 2)
            '   Case " " : Value = "£"
            '  Case "/" : Value = "R"
            ' Case "x" : Value = "S"
            'End Select
            'End If
            If Bold Then Style = Style Or FontStyle.Bold
            If Italic Then Style = Style Or FontStyle.Italic
            Me.Value = Value
            Me.Format = Column.Format & Format
            Me.IsHeader = IsHeader
            If IsHeader Then Tram = "*"
            Do Until InStr(FontSize, "-+") = 0 And InStr(FontSize, "+-") = 0
                FontSize = Replace(FontSize, "-+", "")
                FontSize = Replace(FontSize, "+-", "")
            Loop
            Me.FontSize = FontSize
            Me.Font = New Font(FontName, FontSizeInPoints, Style, GraphicsUnit.Point)
        End Sub

        Function Measure(ByVal Info As PrintInfo) As SizeF
            Dim Height As Single = Info.Graphics.MeasureString(If(InStr(Align, "w") = 0, "1", FormattedValue), Font, Width).Height * Scale
            Indent = (New Font(ReportFontRegular, 8)).GetHeight(Info.Graphics) * 0.1
            Return New SizeF(Width, Height + 2 * Indent)
        End Function

        Sub Print(ByVal Info As PrintInfo, ByVal Height As Single)
            Select Case Type
                Case "text"
                    Dim Rect As New Rectangle(Column.Left, Info.CursorY, Width, Height)
                    Rect.Inflate(-Indent, -Indent)
                    If Tram <> "" Then Info.Add(Rect, Tram)
                    Info.Add(New PageTextItem(Rect, Font, FormattedValue, Align))
                Case "image"
                    Dim Rect As New Rectangle(Column.Left + Width / 2, Info.CursorY, 0, Height)
                    Dim Bitmap As Bitmap = My.Resources.ResourceManager.GetObject(Value)
                    Rect.Inflate(Height / Bitmap.Height * Bitmap.Width / 2, 0)
                    Info.Add(New PageImageItem(Rect, Value))
            End Select
        End Sub

        ReadOnly Property Width() As Single
            Get
                Dim Value As Single = 0
                Dim Column As Integer = Row.Table.Columns.IndexOf(Me.Column)
                For i As Integer = 0 To ColSpan - 1
                    Value += Row.Table.Columns(Column + i).Width
                Next
                Return Value
            End Get
        End Property

        ReadOnly Property FormattedValue() As String
            Get
                If IsDBNull(Value) Then Return ""
                If IsHeader Then Return Value
                Select Case Format
                    Case "d" : Return DMY(Value)
                    Case "m" : Return Fm(Value)
                    Case "n" : Return Fn(Value)
                End Select
                Return Value
            End Get
        End Property

        ReadOnly Property FontSizeInPoints() As Single
            Get
                Select Case FontSize
                    Case "+++" : Return 20
                    Case "++" : Return 17
                    Case "+" : Return 14
                    Case "" : Return 12
                    Case "-" : Return 10
                    Case "--" : Return 9
                    Case "---" : Return 8
                End Select
            End Get
        End Property

    End Class

End Class